package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created by cc
 * Description:
 * User: CZH
 * Date: 2022-09-24
 * Time: 14:32
 */
public class DelOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除图书的名字: ");
        String name = scanner.nextLine();

        int delPos = -1;
        int size = bookList.getBookSize();
        for (int i = 0; i < size; i++) {
            Book book = bookList.getPos(i);
            if (name.equals(book.getName())) {
                delPos = i;
            }
        }
        if (delPos != -1) {
            for (int i = delPos; i < size; i++) {
                Book book = bookList.getPos(i+1);
                bookList.setBooks(delPos, book);
            }
            bookList.setBookSize(size - 1);
            System.out.println("删除成功！");
            return;
        }
        System.out.println("删除失败！");
    }
}
